using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;

using SlimDX;
using SlimDX.Direct3D9;
using SlimDX.Windows;

namespace miciv
{
    public class XmlListenerBMF : XmlListener
    {
        protected BitmapFont m_bmFont;

        public XmlListenerBMF()
        {
            this.m_bmFont = null;
            Subscribe("font", this.fontStartTagEventHandler, this.fontEndTagEventHandler);
            Subscribe("info", this.infoStartTagEventHandler, this.infoEndTagEventHandler);
            Subscribe("common", this.commonStartTagEventHandler, this.commonEndTagEventHandler);
            Subscribe("pages", this.pagesStartTagEventHandler, this.pagesEndTagEventHandler);
            Subscribe("page", this.pageStartTagEventHandler, this.pageEndTagEventHandler);
            Subscribe("chars", this.charsStartTagEventHandler, this.charsEndTagEventHandler);
            Subscribe("char", this.charStartTagEventHandler, this.charEndTagEventHandler);
            Subscribe("kernings", this.kerningsStartTagEventHandler, this.kerningsEndTagEventHandler);
            Subscribe("kerning", this.kerningStartTagEventHandler, this.kerningEndTagEventHandler);
        }

        public void Initialize(BitmapFont _bmFont, CoreContext _coreContext, XmlTraverser _xmlTraverser)
        {
            base.Initialize(_coreContext, _xmlTraverser);
            this.m_bmFont = _bmFont;
        }

        protected bool fontStartTagEventHandler(string _strName, XmlAttributeCollection _xmlAttributes)
        {
            return true;
        }

        protected bool fontEndTagEventHandler(string _strName)
        {
            return true;
        }

        protected bool infoStartTagEventHandler(string _strName, XmlAttributeCollection _xmlAttributes)
        {
            // <info face="Trebuchet MS" size="32" bold="0" italic="0" charset="" unicode="1" stretchH="100" smooth="1" aa="1" padding="0,0,0,0" spacing="1,1" outline="2"/>
            char[] separator = { ',' };
            this.m_bmFont.Info.Face = _xmlAttributes["face"].Value;
            this.m_bmFont.Info.Size = uint.Parse(_xmlAttributes["size"].Value);
            this.m_bmFont.Info.Bold = (0 != uint.Parse(_xmlAttributes["bold"].Value));
            this.m_bmFont.Info.Italic = (0 != uint.Parse(_xmlAttributes["italic"].Value));
            this.m_bmFont.Info.Charset = _xmlAttributes["charset"].Value;
            this.m_bmFont.Info.Unicode = (0 != uint.Parse(_xmlAttributes["unicode"].Value));
            this.m_bmFont.Info.StretchH = uint.Parse(_xmlAttributes["stretchH"].Value);
            this.m_bmFont.Info.Smooth = (0 != uint.Parse(_xmlAttributes["smooth"].Value));
            this.m_bmFont.Info.AA = (0 != uint.Parse(_xmlAttributes["aa"].Value));

            string str = _xmlAttributes["padding"].Value;
            string[] arr = str.Split(separator);
            this.m_bmFont.Info.Padding = new Vector4(float.Parse(arr[0]), float.Parse(arr[1]), float.Parse(arr[2]), float.Parse(arr[3]));

            str = _xmlAttributes["spacing"].Value;
            arr = str.Split(separator);
            this.m_bmFont.Info.Spacing = new Vector2(float.Parse(arr[0]), float.Parse(arr[1]));

            this.m_bmFont.Info.Outline = uint.Parse(_xmlAttributes["outline"].Value);
            return true;
        }

        protected bool infoEndTagEventHandler(string _strName)
        {
            return true;
        }

        protected bool commonStartTagEventHandler(string _strName, XmlAttributeCollection _xmlAttributes)
        {
            // <common lineHeight="32" base="25" scaleW="512" scaleH="256" pages="1" packed="0" alphaChnl="1" redChnl="0" greenChnl="0" blueChnl="0"/>
            this.m_bmFont.Common.LineHeight = uint.Parse(_xmlAttributes["lineHeight"].Value);
            this.m_bmFont.Common.Base = uint.Parse(_xmlAttributes["base"].Value);
            this.m_bmFont.Common.ScaleW = uint.Parse(_xmlAttributes["scaleW"].Value);
            this.m_bmFont.Common.ScaleH = uint.Parse(_xmlAttributes["scaleH"].Value);
            this.m_bmFont.Common.Pages = uint.Parse(_xmlAttributes["pages"].Value);
            this.m_bmFont.Common.Packed = (0 != uint.Parse(_xmlAttributes["packed"].Value));
            this.m_bmFont.Common.AlphaChnl = (0 != uint.Parse(_xmlAttributes["alphaChnl"].Value));
            this.m_bmFont.Common.RedChnl = (0 != uint.Parse(_xmlAttributes["redChnl"].Value));
            this.m_bmFont.Common.GreenChnl = (0 != uint.Parse(_xmlAttributes["greenChnl"].Value));
            this.m_bmFont.Common.BlueChnl = (0 != uint.Parse(_xmlAttributes["blueChnl"].Value));
            return true;
        }

        protected bool commonEndTagEventHandler(string _strName)
        {
            return true;
        }

        protected bool pagesStartTagEventHandler(string _strName, XmlAttributeCollection _xmlAttributes)
        {
            // <pages>
            return true;
        }

        protected bool pagesEndTagEventHandler(string _strName)
        {
            return true;
        }

        protected bool pageStartTagEventHandler(string _strName, XmlAttributeCollection _xmlAttributes)
        {
            // <page id="0" file="Trebuchet.xml_0.tga" />
            return this.m_bmFont.AddPage(uint.Parse(_xmlAttributes["id"].Value), _xmlAttributes["file"].Value);
        }

        protected bool pageEndTagEventHandler(string _strName)
        {
            return true;
        }

        protected bool charsStartTagEventHandler(string _strName, XmlAttributeCollection _xmlAttributes)
        {
            // <chars count="191">
            return true;
        }

        protected bool charsEndTagEventHandler(string _strName)
        {
            return true;
        }

        protected bool charStartTagEventHandler(string _strName, XmlAttributeCollection _xmlAttributes)
        {
            // <char id="32" x="506" y="0" width="5" height="5" xoffset="-2" yoffset="23" xadvance="8" page="0" chnl="15" />
            BitmapFontChar bmChar = new BitmapFontChar();
            bmChar.ID = uint.Parse(_xmlAttributes["id"].Value);
            bmChar.X = uint.Parse(_xmlAttributes["x"].Value);
            bmChar.Y = uint.Parse(_xmlAttributes["y"].Value);
            bmChar.Width = uint.Parse(_xmlAttributes["width"].Value);
            bmChar.Height = uint.Parse(_xmlAttributes["height"].Value);
            bmChar.XOffset = int.Parse(_xmlAttributes["xoffset"].Value);
            bmChar.YOffset = int.Parse(_xmlAttributes["yoffset"].Value);
            bmChar.XAdvance = int.Parse(_xmlAttributes["xadvance"].Value);
            bmChar.Page = uint.Parse(_xmlAttributes["page"].Value);
            bmChar.Chnl = uint.Parse(_xmlAttributes["chnl"].Value);
            this.m_bmFont.AddChar(bmChar);
            return true;
        }

        protected bool charEndTagEventHandler(string _strName)
        {
            return true;
        }

        protected bool kerningsStartTagEventHandler(string _strName, XmlAttributeCollection _xmlAttributes)
        {
            // <kernings count="96">
            return true;
        }

        protected bool kerningsEndTagEventHandler(string _strName)
        {
            return true;
        }

        protected bool kerningStartTagEventHandler(string _strName, XmlAttributeCollection _xmlAttributes)
        {
            // <kerning first="32" second="65" amount="-2" />
            this.m_bmFont.AddKerning(uint.Parse(_xmlAttributes["first"].Value),
                uint.Parse(_xmlAttributes["second"].Value),
                int.Parse(_xmlAttributes["amount"].Value));
            return true;
        }

        protected bool kerningEndTagEventHandler(string _strName)
        {
            return true;
        }
    }
}
